/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.network.aion.clientpackets;

import com.aionengine.gameserver.model.gameobjects.Item;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.templates.item.actions.AbstractItemAction;
import com.aionengine.gameserver.model.templates.item.actions.CosmeticItemAction;
import com.aionengine.gameserver.network.aion.AionClientPacket;
import com.aionengine.gameserver.network.aion.AionConnection.State;
import com.aionengine.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
import com.aionengine.gameserver.services.RenameService;
import com.aionengine.gameserver.utils.PacketSendUtility;

/**
 * @author xTz
 */
public class CM_APPEARANCE extends AionClientPacket {

    private int type;

    private int itemObjId;

    private String name;

    public CM_APPEARANCE(int opcode, State state, State... restStates) {
        super(opcode, state, restStates);
    }

    @Override
    protected void readImpl() {
        type = readC();
        readC();
        readH();
        itemObjId = readD();
        switch (type) {
            case 0:
            case 1:
                name = readS();
                break;
        }

    }

    @Override
    protected void runImpl() {
        final Player player = getConnection().getActivePlayer();

        switch (type) {
            case 0: // Change Char Name,
                if (RenameService.renamePlayer(player, player.getName(), name, itemObjId)) {
                    PacketSendUtility.sendPacket(player, new SM_SYSTEM_MESSAGE(1400157, name));
                }
                break;
            case 1: // Change Legion Name
                if (RenameService.renameLegion(player, name, itemObjId)) {
                    PacketSendUtility.sendPacket(player, new SM_SYSTEM_MESSAGE(1400158, name));
                }
                break;
            case 2: // cosmetic items
                Item item = player.getInventory().getItemByObjId(itemObjId);
                if (item != null) {
                    for (AbstractItemAction action : item.getItemTemplate().getActions().getItemActions()) {
                        if (action instanceof CosmeticItemAction) {
                            if (!action.canAct(player, null, null)) {
                                return;
                            }
                            action.act(player, null, item);
                            break;
                        }
                    }
                }
                break;
        }
    }
}
